package com.jollychic.www.roomdatebasedemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.jollychic.www.roomdatebasedemo.R;
import com.jollychic.www.roomdatebasedemo.fragment.FragmentCreate;
import com.jollychic.www.roomdatebasedemo.fragment.FragmentDelete;
import com.jollychic.www.roomdatebasedemo.fragment.FragmentRetrieve;
import com.jollychic.www.roomdatebasedemo.fragment.FragmentUpdate;

/**
 * desc:  <br/>
 * author: 李超杰 <br/>
 * time: 2018/4/22 17:28 <br/>
 * since: V 6.1X <br/>
 */

public class ActivityRoomMain extends AppCompatActivity implements View.OnClickListener {

    private FragmentCreate mFragmentCreate;
    private FragmentRetrieve mFragmentRetrieve;
    private FragmentUpdate mFragmentUpdate;
    private FragmentDelete mFragmentDelete;

    private Fragment mSignFragment;
    private FragmentManager mFragmentManage;


    private int mCurrentPos;
    private View[] views;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jc_room_root_layout);

        View create = findViewById(R.id.tv_create);
        create.setOnClickListener(this);
        View retrieve = findViewById(R.id.tv_retrieve);
        retrieve.setOnClickListener(this);
        View update = findViewById(R.id.tv_update);
        update.setOnClickListener(this);
        View delete = findViewById(R.id.tv_delete);
        delete.setOnClickListener(this);

        views = new View[4];
        views[0] = create;
        views[1] = retrieve;
        views[2] = update;
        views[3] = delete;

        mFragmentCreate = new FragmentCreate();
        mFragmentRetrieve = new FragmentRetrieve();
        mFragmentUpdate = new FragmentUpdate();
        mFragmentDelete = new FragmentDelete();

        mFragmentManage = getSupportFragmentManager();
        mFragmentManage.beginTransaction().add(R.id.fl_container, mFragmentCreate).commit();
        mSignFragment = mFragmentCreate;
        resetColor();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_create:
                if (mCurrentPos == 0) {
                    return;
                }
                mCurrentPos = 0;
                changeFragment(mSignFragment, mFragmentCreate);
                mSignFragment = mFragmentCreate;
                break;
            case R.id.tv_retrieve:
                if (mCurrentPos == 1) {
                    return;
                }
                mCurrentPos = 1;
                changeFragment(mSignFragment, mFragmentRetrieve);
                mSignFragment = mFragmentRetrieve;
                break;
            case R.id.tv_update:
                if (mCurrentPos == 2) {
                    return;
                }
                mCurrentPos = 2;
                changeFragment(mSignFragment, mFragmentUpdate);
                mSignFragment = mFragmentUpdate;
                break;
            case R.id.tv_delete:
                if (mCurrentPos == 3) {
                    return;
                }
                mCurrentPos = 3;
                changeFragment(mSignFragment, mFragmentDelete);
                mSignFragment = mFragmentDelete;
                break;
            default:
                break;
        }
        resetColor();
    }

    private void resetColor() {
        for (int i = 0; i < views.length; i++) {
            views[i].setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
            if (mCurrentPos == i) {
                views[i].setBackgroundColor(ContextCompat.getColor(this
                        , android.R.color.holo_blue_light));
            }
        }
    }


    private void changeFragment(Fragment hideFragment, Fragment showFragment) {
        if (showFragment.isAdded()) {
            mFragmentManage.beginTransaction()
                    .hide(hideFragment)
                    .show(showFragment)
                    .commit();
        } else {
            mFragmentManage.beginTransaction()
                    .hide(hideFragment)
                    .add(R.id.fl_container, showFragment)
                    .commit();
        }
    }
}
